import tkinter as tk
from PIL import Image,ImageTk
import webbrowser
from os import listdir,system
from random import randint as r
import sqlite3
#运行其他程序
def run(a):
    dire = a
    system('python %s' %dire)
#调整背景图大小
def get_image(filename,width,height):
    im = Image.open(filename).resize((width,height))
    return ImageTk.PhotoImage(im)
#打开帮助页（赞赏）
def help():
    webbrowser.open('https://blog.eastaug.top/archives/77.html',new=0,autoraise=True)
#打开赞助页
def zz():
    webbrowser.open('',new=0,autoraise=True)
#打开bug反馈页
def bug():
    webbrowser.open('https://blog.eastaug.top/archives/91.html',new=0,autoraise=True)
#判断软件状态
a = ''
def check():
    global a
    if check0() == '所有必要文件已准备就绪':
        return 'photo/bg1.png'
    else:
        a = check0()
        return 'photo/bg2.png'
#展示文件丢失页面
def diushi():
    diushi = tk.Tk()
    diushi.title('一些文件因一些不可控原因丢失')
    diushi.geometry('512x400')

    label = tk.Label(diushi,text=a,font=('宋体',12))
    label.pack()

    diushi.mainloop()
#检查文件丢失情况
def check0():#检查文件是否齐全
    func =[]
    py = []
    t=''
    #检查todo/functions路径
    lit = listdir('todo/functions')
    func_lit = ['exec.py','run.py','sql.py','__pycache__']#todo/functions必要的文件
    for i in func_lit:
        if i not in lit:
            func.append(i)

    #检查todo/functions/__pycache__路径
    lit = listdir('todo/functions/__pycache__') 
    pycache_lit = ['check.cpython-311.pyc','exec.cpython-310.pyc','exec.cpython-311.pyc','exec.cpython-38.pyc','exec.cpython-39.pyc','sql.cpython-310.pyc','sql.cpython-311.pyc','sql.cpython-38.pyc','sql.cpython-39.pyc']
    for i in pycache_lit:
        if i not in lit:
            py.append(i)
    
    if func == [] and py == []:
        return '所有必要文件已准备就绪'
    else:
        if func != []:
            for i in func:
                t += (str(i)+'\n')
            a ='在软件目录todo/functions下，丢失以下文件：\n'+t
            return a
            
        if py != []:
            for i in py:
                t+= (str(i)+'\n')
            a ='在软件目录todo/functions/__pycache__下，丢失以下文件：\n'+t
            return a
#跳转到查询任务

#跳转到编辑任务

#跳转到删除任务

#跳转到手动删除
window= tk.Tk()
window.title('Data-Backup-CTRL_2.0')
window.geometry('1024x728')

#设置menu栏
menu=tk.Menu(window)

wenjian=tk.Menu(menu,tearoff=0)
wenjian.add_command(label='用户表另存为',font=("宋体",8,"normal"))
menu.add_cascade(label="文件",menu=wenjian,font=("宋体",12,"normal"))

bangzhu = tk.Menu(menu,tearoff=0)
bangzhu.add_command(label='使用说明',command=help,font=('黑体',8,'normal'))
menu.add_cascade(label='帮助',menu=bangzhu,font=('宋体',12,'normal'))

caozuo = tk.Menu(menu,tearoff=0)
caozuo.add_command(label='创建任务',command=lambda: run(r'todo/views/creative.py'),font=('宋体',8,'normal'))
caozuo.add_command(label='查询任务',command=help,font=('宋体',8,'normal'))
caozuo.add_command(label='编辑任务',command=help,font=('宋体',8,'normal'))
caozuo.add_command(label='删除任务',command=help,font=('宋体',8,'normal'))
caozuo.add_command(label='手动备份',command=help,font=('宋体',8,'normal'))
menu.add_cascade(label='操作',menu=caozuo,font=('宋体',12,'normal'))

zanzhu = tk.Menu(menu,tearoff=0)
zanzhu.add_command(label='赞助',command=zz,font=('宋体',8,'normal'))
menu.add_cascade(label='赞助',menu=zanzhu,font=('宋体',12,'normal'))
window.config(menu=menu)

#设置背景图片
canvas_root = tk.Canvas(window,width=1024,height=728)
img = r(1,16)
if a == '':
    im_root = get_image(check(),1024,728)
else:
    im_root = get_image(check(),1024,728)
canvas_root.create_image(512,364,image = im_root)



#设置组件
im_label1 = get_image('photo/bt.png',1024,70)
label = tk.Label(window,image=im_label1)


im_button1 = get_image('photo/4.png',250,50)
button1 = tk.Button(window,image=im_button1,compound=tk.CENTER,command=help)

im_button2 = get_image('photo/5.png',250,50)
button2 = tk.Button(window,image=im_button2,compound=tk.CENTER,command=bug)

im_button3 = get_image('photo/10.png',250,50)
button3 = tk.Button(window,image=im_button3,compound=tk.CENTER,command=diushi)
'''
Button1 = tk.Button(window,text='创建任务',width=30,height=3)
Button2 = tk.Button(window,text='查询任务',width=30,height=3)
Button3 = tk.Button(window,text='编辑任务',width=30,height=3)
Button4 = tk.Button(window,text='删除任务',width=30,height=3)
Button5 = tk.Button(window,text='手动备份',width=30,height=3)
Button6 = tk.Button(window,text='赞助',width=30,height=3)

Button1.place(x=150,y=250)
Button2.place(x=400,y=250)
Button3.place(x=650,y=250)
Button4.place(x=150,y=450)
Button5.place(x=400,y=450)
Button6.place(x=650,y=450)

'''
label.place(y=0)
canvas_root.pack()
button1.place(x=700,y=550)
button2.place(x=700,y=650)
if a != '':
    button3.place(x=500,y=400)

window.mainloop()              